/* eslint-disable max-lines */
import TauSimpleSelect from '@/components/TauSimpleSelect';
import { ellipsisRender } from '@/components/TauTable';
import * as BASE from '@/stores/definitions-base';
import { Input, Tag } from '@alifd/next';
import { t } from '@lingui/macro';
import React from 'react';

const {
  DATETIME_TEXT,
  DATETIMES_TEXT,
  DATE_TEXT,
  TIME_TEXT,
  EMPTY_TEXT,
  ENUMS: ENUMSBase,
  FieldsGrid: FieldGridBase,
  TitleGrid,
  column: columnBase,
  toSelectDataSource,
} = BASE;
export { DATETIME_TEXT, DATETIMES_TEXT, DATE_TEXT, EMPTY_TEXT, TIME_TEXT, TitleGrid, toSelectDataSource };

/**
 * @param {*} path        属性path
 * @param {*} props       column属性
 * @returns column定义
 */
export function column(...args) {
  return columnBase(model, ...args);
}

/**
 * 显示一组表单字段
 * ```js
 * {
 *   fields: string[],    // 字段名称，可以使用.来指定嵌套对象属性，例如order.busOrder.shift
 *   columns: number,     // 列数，默认为4,最大为12,最小为1
 *   model: object,       // 字段定义数据模型的根对象，默认不指定，仅在使用子对象作为根模型时设置
 *   overrideModel: object, // 重载字段定义数据模型
 *   mode: string,        // 表单模式，默认为edit，可选view
 *   itemLayout: object,  // FormItem布局配置，默认为{}
 * }
 * ```
 *
 * @param {object} param0
 * @returns
 */
export function FieldsGrid(props) {
  return <FieldGridBase model={model} {...props} />;
}

//----------------------------------------------------------------------
export const ENUMS = {
  ...ENUMSBase,
  // <br/> 1-转入<br/> 0-转出
  type: {
    1: t`转入`,
    0: t`转出`,
  },

  type_text: {
    1: { label: t`转入`, color: 'green' },
    0: { label: t`转出`, color: 'yellow' },
  },
};

/**
 * 模型定义：
 * 1. 普通文本仅需要定义label
 * 2. 需要通过枚举转换的值，定义text; 当使用renderer返回组件时，text属性无效，需要通过组件的renderPreview来指定预览时的绘制方式
 * 3. 通用的ui属性需要定制时，定义ui; 特定模式(view, edit, add)下的ui，添加后缀定义为ui_<mode>，例如：ui_view, ui_edit, ui_add, ui_table
 * 4. ui可配置属性包括:
 *    renderer: 内容渲染函数, Form表单中的原型为：(value: any, formData: any, fieldDefinition: any) => ReactNode; Table Column中的原型为: (value: any, columnIndex: number, rowData: any) => ReactNode;
 *    itemProps: 表单Form.Item的属性，可以是对象，或函数({ isPreview: boolean, mode: string, value: any, formData: any, ...otherDefinitions}) => void;
 *    span: Grid Cell占用的列数, 默认为1
 *
 * 【注】获取接口的model定义后，vscode中通过正则替换：^(\s+)"label": "([^']+)"  ===> $1label: t`$2`
 */
export const model = {
  id: {
    label: '',
  },
  issuePhone: {
    label: t`发起人成员手机号(没有则不填)`,
  },
  issueName: {
    label: t`发起人名称`,
  },
  issueId: {
    label: t`发起人`,
    ui: {
      renderer: (v) => (
        <TauSimpleSelect
          value={v}
          data={rootStore.transferStore.issues}
          showSearch
        />
      ),
      itemProps: { required: true },
    },
    ui_view: {
      renderer: (v) => (
        <TauSimpleSelect
          value={v}
          data={rootStore.transferStore.issues}
        />
      ),
    },
  },
  groupId: {
    label: t`群名称`,
    ui: {
      itemProps: { required: true },
    },
  },
  groupName: {
    label: t`群名`,
  },
  type: {
    label: t`类型`,
    text: (v) => <Tag key={`p_p_${ENUMS.type_text[v].color}`} size="small" type="primary" color={ENUMS.type_text[v].color}>{ENUMS.type_text[v].label}</Tag>,
    ui: {
      renderer: (v) => (<TauSimpleSelect data={toSelectDataSource(ENUMS.type)} value={v} />),
      itemProps: { required: true },
    },
  },
  points: {
    label: t`转移积分`,
    ui: {
      renderer: (v) => <Input value={v} placeholder="请输入积分" />,
      itemProps: { required: true },
    },
  },
  memberId: {
    label: t`转移人`,
    ui: {
      renderer: (v) => (
        <TauSimpleSelect
          value={v}
          data={rootStore.transferStore.transfers}
          showSearch
        />
      ),
      itemProps: { required: true },
    },
    ui_view: {
      renderer: (v) => (
        <TauSimpleSelect
          value={v}
          data={rootStore.transferStore.transfers}
        />
      ),
    },
  },
  memberPhone: {
    label: t`转移方电话(没有则不填)`,
  },
  memberName: {
    label: t`转移人`,
  },
  remarks: {
    label: t`备注`,
    text: (v) => <>{ellipsisRender(v)}</>,
    ui: {
      renderer: (v) => <Input.TextArea value={v} />,
    },
  },
  crBy: {
    label: t`创建者`,
  },
  upBy: {
    label: t`最后修改者`,
  },
  crDate: {
    label: t`创建时间`,
    text: DATETIME_TEXT,
  },
  upDate: {
    label: t`最后修改时间`,
  },
  tenantId: {
    label: t`租户ID`,
  },
  memberGroup: {
    label: t`转移人编号 / 昵称`,
    text: (v, r) => <>{r?.memberId} / {r?.memberName}</>,
  },
};
